﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace MetalSoda
{

    [Serializable]
    public struct InstanceSetupInfo
    {
        /// <summary>
        /// EndPoint: SERVICE_BROKER
        /// </summary>
        public const string DEFAULT_SSB_ENDPOINT_NAME = "SERVICE_BROKER";

        /// <summary>
        /// Port: 20140
        /// </summary>
        public const int DEFAULT_SSB_PORT = 20140;

        #region Private Fields
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private bool m_overwriteServerSettings;

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private bool m_autoSelectAvailablePort;

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private string m_ssbEndPointName;

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private int? m_ssbEndPointPort; 

        #endregion

        #region Properties

        public bool OverwriteServerSettings
        {
            [DebuggerStepThrough]
            get { return m_overwriteServerSettings; }
            [DebuggerStepThrough]
            set { m_overwriteServerSettings = value; }
        }

        public bool AutoSelectAvailablePort
        {
            [DebuggerStepThrough]
            get { return m_autoSelectAvailablePort; }
            [DebuggerStepThrough]
            set { m_autoSelectAvailablePort = value; }
        }

        public string SSBEndPointName
        {
            [DebuggerStepThrough]
            get { return m_ssbEndPointName ?? (m_ssbEndPointName = InstanceSetupInfo.DEFAULT_SSB_ENDPOINT_NAME); }
            [DebuggerStepThrough]
            set
            {
                Regex regex = new Regex(@"^\w[-\.\w]*$");
                if (regex.IsMatch(value ?? string.Empty))
                {
                    throw new FormatException();
                }
                m_ssbEndPointName = value;
            }
        }

        public int SSBEndPointPort
        {
            [DebuggerStepThrough]
            get { return (m_ssbEndPointPort ?? (m_ssbEndPointPort = InstanceSetupInfo.DEFAULT_SSB_PORT)).Value; }
            [DebuggerStepThrough]
            set
            {
                if (!Utils.IsValidPort(value)) { throw new ArgumentOutOfRangeException(); }
                m_ssbEndPointPort = value;
            }
        } 
        #endregion
    }
}
